package com.kede.api.stand.service.impl.zhi;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kede.api.stand.dto.CateDTO;
import com.kede.api.stand.service.zhi.IZhiManageService;
import com.kede.dictenum.BoolDictEnum;
import com.kede.domain.ZhiActionDomain;
import com.kede.domain.ZhiCategoryDomain;
import com.kede.domain.ZhiImgsDomain;
import com.kede.pojo.ResHttp;
import com.kede.service.ConfigService;
import com.kede.service.ZhiActionService;
import com.kede.service.ZhiCategoryService;
import com.kede.service.ZhiImgsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author:Pr.li
 * @Date: create in 2023/7/8 16:52
 */
@Service
public class ZhiManageServiceImpl implements IZhiManageService {

    @Resource
    private ZhiCategoryService zhiCategoryService;

    @Resource
    private ZhiActionService zhiActionService;

    @Resource
    private ZhiImgsService zhiImgsService;

    @Resource
    private ConfigService configService;


    @Override
    public ResHttp getNextCateById(Integer cateId, Integer isWeight) {
        LambdaQueryWrapper<ZhiCategoryDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZhiCategoryDomain::getPid,cateId);
        queryWrapper.eq(ZhiCategoryDomain::getIsWeight,isWeight);
        queryWrapper.eq(ZhiCategoryDomain::getIsActive, BoolDictEnum.TRUE.getCode());
        List<ZhiCategoryDomain> list = zhiCategoryService.list(queryWrapper);
        return ResHttp.okData(new CateDTO(list));
    }

    @Override
    public ResHttp getCateInfoById(Integer cateId) {
        ZhiCategoryDomain src = zhiCategoryService.getById(cateId);
        ZhiCategoryDomain target = new ZhiCategoryDomain();
        BeanUtil.copyProperties(src,target);
        paddingCate(src,target);
        return ResHttp.okData(target);
    }

    @Override
    public ResHttp getZhiHotLit() {
        LambdaQueryWrapper<ZhiCategoryDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZhiCategoryDomain::getIsActive,BoolDictEnum.TRUE.getCode());
        queryWrapper.eq(ZhiCategoryDomain::getIsHot,BoolDictEnum.TRUE.getCode());
        queryWrapper.eq(ZhiCategoryDomain::getIsWeight,BoolDictEnum.FALSE.getCode());
        return ResHttp.okData(zhiCategoryService.list(queryWrapper));
    }

    private static BigDecimal AsNUll = new BigDecimal("-1");

    private boolean canDeepCate(BigDecimal target){
        return target == null || target.compareTo(AsNUll) == 0;
    }

    /**
     * 通过递归循环填充 target完整属性
     * @param src
     * @param target
     */
    private void paddingCate(ZhiCategoryDomain src,ZhiCategoryDomain target){
        boolean goParent = false;
        if(canDeepCate(target.getDefaultQuMoney())){
            if(!canDeepCate(src.getDefaultQuMoney())){
                target.setDefaultQuMoney(src.getDefaultQuMoney());
            }else{
                goParent = true;
            }
        }
        if(canDeepCate(target.getDefaultShiMoney()) ){
            if(!canDeepCate(src.getDefaultShiMoney())){
                target.setDefaultShiMoney(src.getDefaultShiMoney());
            }else{
                goParent = true;
            }
        }
        if(canDeepCate(target.getDefaultStoreMoney())){
            if(!canDeepCate(src.getDefaultStoreMoney())){
                target.setDefaultStoreMoney(src.getDefaultStoreMoney());
            }else{
                goParent = true;
            }
        }
        if(canDeepCate(target.getDefaultUserMoney()) ){
            if(!canDeepCate(src.getDefaultUserMoney())){
                target.setDefaultUserMoney(src.getDefaultUserMoney());
            }else{
                goParent = true;
            }
        }
        if(canDeepCate(target.getDefaultUserDebi()) ){
            if(!canDeepCate(src.getDefaultUserDebi())){
                target.setDefaultUserDebi(src.getDefaultUserDebi());
            }else{
                goParent = true;
            }
        }
        if(target.getZhiActionList() == null ){
            List<ZhiActionDomain> action = getActionsByNodeId(src.getId());
            if(action !=null && action.size() > 0){
                target.setZhiActionList(action);
            }else{
                goParent = true;
            }
        }
        if(target.getImgs() == null ){
            ZhiImgsDomain zhiImgs = getImgsByNodeId(src.getId());
            if(zhiImgs != null){
                target.setImgs(zhiImgs);
            }else{
                goParent = true;
            }
        }
        if(goParent){
            if(src.getPid().equals(0)){
                return;
            }
            ZhiCategoryDomain byId = zhiCategoryService.getById(src.getPid());
            if(byId != null){
                paddingCate(byId,target);
            }
        }
    }



    private List<ZhiActionDomain> getActionsByNodeId(Integer id){
        LambdaQueryWrapper<ZhiActionDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZhiActionDomain::getNodeId,id);
        return zhiActionService.list(queryWrapper);
    }

    private ZhiImgsDomain getImgsByNodeId(Integer id){
        LambdaQueryWrapper<ZhiImgsDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZhiImgsDomain::getNodeId,id);
        return zhiImgsService.getOne(queryWrapper);
    }
}
